package com.example.gofeatureflag;

import dev.openfeature.sdk.Client;
import dev.openfeature.sdk.EvaluationContext;
import dev.openfeature.sdk.MutableContext;
import dev.openfeature.sdk.OpenFeatureAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestHello {
  private final OpenFeatureAPI openFeatureAPI;

  @Autowired
  public RestHello(OpenFeatureAPI OFApi) {
    this.openFeatureAPI = OFApi;
  }

  @GetMapping("/hello")
  public String getHello() {
    final Client client = openFeatureAPI.getClient();

    EvaluationContext userContext = new MutableContext("1d1b9238-2591-4a47-94cf-d2bc080892f1")
        .add("firstname", "john")
        .add("lastname", "doe")
        .add("email","john.doe@gofeatureflag.org")
        .add("admin", true)
        .add("anonymous", false);

    // Evaluate welcome-message feature flag
    if (Boolean.TRUE.equals(client.getBooleanValue("flag-only-for-admin", false, userContext))) {
      return "Hello, welcome to this OpenFeature-enabled website!";
    }


    return "Hello!";
  }

  @GetMapping("/github")
  public String getGithub() {
    final Client client = openFeatureAPI.getClient();

    EvaluationContext userContext = new MutableContext("1d1b9238-2591-4a47-94cf-d2bc080892f1")
        .add("firstname", "john")
        .add("lastname", "doe")
        .add("email","john.doe@gofeatureflag.org")
        .add("admin", true)
        .add("anonymous", false);

    // Evaluate welcome-message feature flag
    if (Boolean.TRUE.equals(client.getBooleanValue("test-flag", false, userContext))) {
      return "Hello, welcome to this OpenFeature-enabled website!";
    }


    return "Hello!";
  }
}